<?php

namespace App\Miya\UIData;

use Illuminate\Support\Facades\Cache;
use ReflectionMethod;
use ReflectionClass;

class CommonData
{
    protected $name;
    protected $fields;

    /**
     * 当前工具类的说明数据
     */
    public function helpData()
    {
        $docs = [];
        $ref = new ReflectionClass(get_class($this));
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
        collect($methods)->each(function (ReflectionMethod $method) use (&$docs) {
            if ($method->name == 'helpData') {
                return;
            }
            $docs[$method->name] = [
                'parms' => collect($method->getParameters())->map(function ($parm) {
                    return sprintf('$%s', $parm->name);
                })->toArray(),
                'doc' => $this->parseDocComment($method->getDocComment())
            ];
        });

        return $docs;
    }

    /**
     * 格式化备注信息
     *
     * @param $doc
     * @return string
     */
    protected function parseDocComment($doc)
    {
        return trim(str_replace(['/**', '*/', '*', '    ', '  '], '', $doc));
    }

    /**
     * 数据缓存
     *
     * @param $func
     * @param $method
     * @param null $tag
     * @return mixed
     */
    protected function dataCache($func, $method, $tag = null)
    {
        $cacheKey = sprintf('%s_%s', $method, $tag);
        return Cache::remember($cacheKey, config('setting.cache_ttl'), $func);
    }
}
